home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Today - The Disc! 5 / CD-ROM Today - The Disc (Issue 5)(November 1994).ISO / mac / Mac shareware / Education / RLaB / rlib / complement.r < prev    next >
Text File  |  1994-09-21  |  780b  |  36 lines

  1. //-------------------------------------------------------------------//
  2. //  complement
  3.  
  4. //  Syntax:    complement ( A , B )
  5.  
  6. //  Description:
  7.  
  8. //  The complement function returns a vector-set that contains the
  9. //  complement of A in B. In plain English: the elements of B that are
  10. //  not in A.
  11.  
  12. //  See Also: intersection, set, union
  13. //-------------------------------------------------------------------//
  14.  
  15. complement = function ( A, B )
  16. {
  17.   local (Comp, a, i)
  18.  
  19.   if (A.n == 0) { return [B]; }
  20.  
  21.   if (min (size (A)) != 1) {
  22.     error ("complement: 1st arg must be a vector");
  23.   }
  24.  
  25.   if (min (size (B)) != 1) {
  26.     error ("complement: 2st arg must be a vector");
  27.   }
  28.  
  29.   a = set (A); Comp = set (B);
  30.   for (i in 1:a.n)
  31.   {
  32.     Comp = Comp[ find (a[i] != Comp) ];
  33.   }
  34.   return Comp
  35. };
  36.